package com.lp.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

@Slf4j
public class HttpBaseUtils extends ConnectUtil {

    public static JSONObject connectGetForObject(String url, Map<String, String> heads) {
        return JSONObject.parseObject(connectGet(url, heads));
    }

    public static JSONObject connectGetForObject(String url) {
        return connectGetForObject(url, null);
    }

    public static JSONObject connectPostForObject(String url, JSONObject json, Map<String, String> heads) {
        return JSONObject.parseObject(connectPost(url, json, heads));
    }

    public static JSONObject connectPostForObject(String url, JSONObject json) {
        return connectPostForObject(url, json, null);
    }

    public static JSONObject connectPostForObject(String url, Map<String, String> heads) {
        return connectPostForObject(url, new JSONObject(), heads);
    }


    public static JSONObject connectPostForObject(String url) {
        return connectPostForObject(url, new JSONObject());
    }


    public static JSONObject connectHttpsGetForObject(String url, Map<String, String> heads) {

        return JSONObject.parseObject(connectHttpsGet(url, heads));
    }

    public static JSONObject connectHttpsGetForObject(String url) {
        return connectHttpsGetForObject(url, null);
    }


    public static JSONObject connectHttpsPostForObject(String url, JSONObject json, Map<String, String> heads) {
        return JSONObject.parseObject(connectHttpsPost(url, json, heads));
    }

    public static JSONObject connectHttpsPostForObject(String url, JSONObject json) {
        return connectHttpsPostForObject(url, json, null);
    }

    public static JSONObject connectHttpsPostForObject(String url, Map<String, String> heads) {
        return connectHttpsPostForObject(url, new JSONObject(), heads);
    }

    public static JSONObject connectHttpsPostForObject(String url) {
        return connectHttpsPostForObject(url, new JSONObject(), null);
    }

    /**
     * 向远程地址发送get请求
     * @param url
     * @return
     */
    public static JSONObject connectionGetForObject(String url){
        JSONObject json = null;
        try {
            URL urlConnection = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlConnection.openConnection();
            conn.setConnectTimeout(10000); // 连接超时设置为5秒
            conn.setReadTimeout(5000);    // 读取超时设置为5秒
            conn.setRequestMethod("GET");

            int responseCode = conn.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                json = JSONObject.parseObject(response.toString());
            } else {
                log.error("网络请求异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

}
